<?php
/**
 * 订单支付记录控制器
 */
declare(strict_types=1);

namespace app\controller\crm;

use app\BaseController;
use app\service\OrderService;
use app\model\crm\OrderPayment;
use think\Response;

class OrderPaymentController extends BaseController
{
    protected $noNeedLogin = [];
    
    protected OrderService $orderService;
    
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->orderService = new OrderService();
    }
    
    /**
     * 新增支付记录
     * @return Response
     */
    public function add(): Response
    {
        try {
            $params = $this->request->post();
            $userId = $this->getUserId();
            
            // 数据验证
            if (empty($params['order_id'])) {
                return json([
                    'code' => 400,
                    'msg'  => '订单ID不能为空',
                    'data' => null,
                    'timestamp' => time()
                ]);
            }
            
            if (empty($params['payment_amount']) || $params['payment_amount'] <= 0) {
                return json([
                    'code' => 400,
                    'msg'  => '支付金额必须大于0',
                    'data' => null,
                    'timestamp' => time()
                ]);
            }
            
            $result = $this->orderService->addPaymentRecord(
                (int)$params['order_id'],
                $params,
                $userId
            );
            
            return json([
                'code' => 0,
                'msg'  => '支付记录添加成功',
                'data' => $result,
                'timestamp' => time()
            ]);
            
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'msg'  => $e->getMessage(),
                'data' => null,
                'timestamp' => time()
            ]);
        }
    }
    
    /**
     * 获取订单支付记录列表
     * @return Response
     */
    public function list(): Response
    {
        try {
            $orderId = $this->request->get('order_id/d', 0);
            
            if (!$orderId) {
                return json([
                    'code' => 400,
                    'msg'  => '订单ID不能为空',
                    'data' => null,
                    'timestamp' => time()
                ]);
            }
            
            $list = OrderPayment::where('order_id', $orderId)
                ->where('delete_time', 0)
                ->order('payment_date', 'desc')
                ->select()
                ->toArray();
            
            return json([
                'code' => 0,
                'msg'  => 'success',
                'data' => $list,
                'timestamp' => time()
            ]);
            
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'msg'  => $e->getMessage(),
                'data' => null,
                'timestamp' => time()
            ]);
        }
    }
    
    /**
     * 核实支付记录
     * @param int $id
     * @return Response
     */
    public function verify(int $id): Response
    {
        try {
            $userId = $this->getUserId();
            
            $payment = OrderPayment::find($id);
            if (!$payment) {
                return json([
                    'code' => 404,
                    'msg'  => '支付记录不存在',
                    'data' => null,
                    'timestamp' => time()
                ]);
            }
            
            if ($payment->verification_status == OrderPayment::VERIFICATION_VERIFIED) {
                return json([
                    'code' => 400,
                    'msg'  => '该支付记录已核实',
                    'data' => null,
                    'timestamp' => time()
                ]);
            }
            
            $payment->verify($userId);
            
            return json([
                'code' => 0,
                'msg'  => '支付记录核实成功',
                'data' => null,
                'timestamp' => time()
            ]);
            
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'msg'  => $e->getMessage(),
                'data' => null,
                'timestamp' => time()
            ]);
        }
    }
}
